package cc.site.main.service.impl;

import cc.site.main.bean.TblOrder;
import cc.site.main.bean.TblUser;
import cc.site.main.bean.dto.OrderDto;
import cc.site.main.bean.dto.PetDto;
import cc.site.main.mapper.TblOrderMapper;
import cc.site.main.service.TblOrderService;
import cc.site.main.service.TblPetService;
import cc.site.main.service.TblUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangruomao
 * @since 2024-04-14
 */
@Service
public class TblOrderServiceImpl extends ServiceImpl<TblOrderMapper, TblOrder> implements TblOrderService {

    @Autowired
    private TblUserService userService;

    @Autowired
    private TblPetService petService;

    @Override
    public List<OrderDto> listInfo() {
        List<OrderDto> list = new ArrayList<>();

        Map<Integer, TblUser> userMap = userService.list().stream()
                .collect(Collectors.toMap(x -> x.getId(), x -> x));
        Map<Integer, PetDto> petMap = petService.all().stream()
                .collect(Collectors.toMap(x -> x.getId(), x -> x));

        list().forEach(e -> {
            OrderDto dto = new OrderDto();
            BeanUtils.copyProperties(e, dto);

            Integer userId = e.getUserId();
            Integer petId = e.getPetId();

            dto.setUser(userMap.get(userId));
            dto.setPet(petMap.get(petId));

            list.add(dto);
        });

        return list;
    }
}
